<template>
  <Card v-show="currentState === 'edit' || currentState === 'add'" style="margin-top: 10px">
    <p slot="title">
      <Icon :type="icon"></Icon>
      {{title}}
    </p>
    <Row>
      <Col span="12">
        <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="80">
          <Form-item label="文件标题" prop="title">
            <Input type="text" v-model="formItem.title"></Input>
          </Form-item>
          <Form-item label="文件描述" prop="mark">
            <Input type="textarea" v-model="formItem.mark"></Input>
          </Form-item>
          <Form-item label="上传文件" prop="file">
            <Upload
              ref="upload"
              type="drag"
              :headers="header"
              :on-error="error"
              :on-success="success"
              :on-remove="remove"
              :on-format-error="formatError"
              :on-exceeded-size="exceededSize"
              :format="format"
              :max-size="size"
              :default-file-list="formItem.appendixs"
              :action="url">
              <div style="padding: 20px 0">
                <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                <p>点击或拖拽文件上传</p>
              </div>
            </Upload>
          </Form-item>
          <Form-item>
            <Button type="primary" @click="saverOrUpdate">保存</Button>
            <Button @click="back" style="margin-left: 8px">返回</Button>
          </Form-item>
        </Form>
      </Col>
    </Row>
  </Card>
</template>

<script>
import { getToken } from '@/libs/util'
import config from '@/config'
import { deleteAppendix } from '../../../api/sys/appendix'
import { saveOrUpdateSysFile, getSysFileInfo } from '../../../api/sys/download'

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
export default {
  name: 'download-save-update',
  data () {
    return {
      url: baseUrl + 'sys/appendix/upload',
      size: 102400,
      format: config.format,
      currentState: '',
      formItem: {},
      ruleValidate: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    header: function () {
      return {
        'x-access-token': getToken()
      }
    },
    title () {
      return this.currentState === 'edit' ? '编辑用户' : (this.currentState === 'add' ? '新增用户' : '')
    },
    icon () {
      return this.currentState === 'edit' ? 'md-create' : (this.currentState === 'add' ? 'md-add' : '')
    }
  },
  methods: {
    exceededSize () {
      this.$Message.error('文件大小不能超过10M')
    },
    formatError () {
      this.$Message.error('不支持该文件格式')
    },
    error (response, file, fileList) {
      this.$Message.error(response)
    },
    success (response, file, fileList) {
      if (response.code === 200) {
        file.id = response.appendixId
      } else {
        this.$Message.error('文件上传失败')
        this.$refs['upload'].clearFiles()
      }
    },
    remove (file, fileList) {
      deleteAppendix(file.id).then(res => {
      })
    },
    saverOrUpdate () {
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          this.formItem.appendixs = this.$refs['upload'].fileList
          saveOrUpdateSysFile(this.formItem).then(res => {
            this.currentState = 'normal'
            this.$emit('showTable')
          })
        }
      })
    },
    back () {
      this.currentState = 'normal'
      this.$emit('showTable')
    },
    showAdd () {
      this.formItem = {}
      this.currentState = 'add'
      this.$emit('hideTable')
    },
    showEdit (id) {
      getSysFileInfo(id).then(res => {
        if (res.code === 200) {
          this.formItem = res.file
          this.currentState = 'edit'
          this.$emit('hideTable')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
